package com.gyf.mybatis.dao;

import com.gyf.mybatis.io.Resource;
import com.gyf.mybatis.pojo.User;
import com.gyf.mybatis.sqlsession.SqlSession;
import com.gyf.mybatis.sqlsession.SqlSessionBuilder;
import com.gyf.mybatis.sqlsession.SqlSessionFactory;

import java.io.InputStream;
import java.util.List;

public class UserDao implements IUserDao {

    @Override
    public List<User> findAll() throws Exception {
        InputStream inputStream = Resource.getResourceAsStream("/SqlMapConfig.xml");
        SqlSessionFactory factory = new SqlSessionBuilder().build(inputStream);
        SqlSession sqlSession = factory.openSession();
        User user = new User();
        user.setId(1L);
        user.setName("gyf");
        return sqlSession.selectList("user.selectList", user);
    }

    @Override
    public User findByCondition(User user) throws Exception {
        InputStream inputStream = Resource.getResourceAsStream("/SqlMapConfig.xml");
        SqlSessionFactory factory = new SqlSessionBuilder().build(inputStream);
        SqlSession sqlSession = factory.openSession();
        return sqlSession.selectOne("user.selectOne", user);
    }
}
